package com.tools.ad.util;

import android.os.Message;
import android.support.annotation.StringRes;

import com.hm.base.android.mob.AMApplication;
import com.tools.ad.ADGAConstant;
import com.tools.ad.AdType;
import com.tools.ad.R;
import com.tools.ad.bean.AdGARecord;

/**
 * Created by ChenLi on 2018/1/25.
 */

public class GARecordUtils {

    public static void report(AMApplication amApplication, int msgid, String categoryId, String actionId, String labelId, Object obj) {
        Message msg = new Message();
        msg.what = msgid;
        AdGARecord record = new AdGARecord(categoryId, actionId, labelId);
        record.setObj(obj);
        msg.obj = record;
        amApplication.handleMobMessage(msg);
    }

    public static void report(AMApplication amApplication, int msgid, String categoryId, String actionId, String labelId) {
        Message msg = new Message();
        msg.what = msgid;
        AdGARecord record = new AdGARecord(categoryId, actionId, labelId);
        msg.obj = record;
        amApplication.handleMobMessage(msg);

    }

    public static void report(AMApplication amApplication, String categoryId, String actionId, String labelId) {
        Message msg = new Message();
        msg.what = R.id.msg_ad_ga_record;
        AdGARecord record = new AdGARecord(categoryId, actionId, labelId);
        msg.obj = record;
        amApplication.handleMobMessage(msg);
    }

    public static void report(AMApplication amApplication, String categoryId, String actionId, String labelId, Object obj) {
        Message msg = new Message();
        msg.what = R.id.msg_ad_ga_record;
        AdGARecord record = new AdGARecord(categoryId, actionId, labelId);
        record.setObj(obj);
        msg.obj = record;
        amApplication.handleMobMessage(msg);
    }

    public static void reportValue(AMApplication amApplication, String categoryId, String actionId, String labelId, long v, Object obj) {
        Message msg = new Message();
        msg.what = R.id.msg_ad_ga_record;
        AdGARecord record = new AdGARecord(categoryId, actionId, labelId, v);
        record.setObj(obj);
        msg.obj = record;
        amApplication.handleMobMessage(msg);
    }

    public static void onADClick(AMApplication imContext, String adSceneName, AdType adType) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_CLICK, R.string.GA_record_ad_on_click, adSceneName, adType);
    }

    public static void onADShow(AMApplication imContext, String adSceneName, AdType type) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_SHOW, R.string.GA_record_ad_on_show, adSceneName, type);
    }

    public static void onADShowFailed(AMApplication imContext, String adSceneName, AdType type, int error) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_SHOW, R.string.GA_record_ad_on_show_failed, adSceneName, type, error);
    }

    public static void onADLoad(AMApplication imContext, String adSceneName, AdType type) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_REQUEST, R.string.GA_record_ad_on_load, adSceneName, type);
    }

    public static void onADLoadByActivity(AMApplication imContext, String adSceneName, AdType type) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_REQUEST, R.string.GA_record_ad_on_load_activity, adSceneName, type);
    }

    public static void onADFilled(AMApplication imContext, String adSceneName, AdType type) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_REQUEST, R.string.GA_record_ad_on_filled, adSceneName, type);
    }

    public static void onADShowEffective(AMApplication imContext, String name) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_SHOW, R.string.GA_record_ad_on_show_effective, name, AdType.ALL);
    }

    public static void onRequestEffective(AMApplication imContext, String adSceneName) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_REQUEST, R.string.GA_record_ad_on_load_effective, adSceneName, AdType.ALL);
    }

    public static void onADLoad(AMApplication imContext, String adSceneName, AdType type, int code) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_REQUEST, R.string.GA_record_ad_on_load, adSceneName, type, code);
    }

    public static void onADFail(AMApplication imContext, String adSceneName, AdType type, int code) {
        report(imContext, ADGAConstant.C_AD, ADGAConstant.A_REQUEST, R.string.GA_record_ad_on_fail, adSceneName, type, code);
    }

    public static void report(AMApplication imContext, String category, String action, @StringRes int label, String adSceneName, AdType type) {
        if (AdType.ALL.equals(type)) {
            report(imContext, category, action, imContext.getString(label, adSceneName));
        } else {
            report(imContext, category, action, imContext.getString(label, type.getName(), adSceneName));
        }
    }

    public static void report(AMApplication imContext, String category, String action, @StringRes int label, String adSceneName, AdType type, int code) {
        report(imContext, category, action, imContext.getString(label, type.getName(), adSceneName) + "_(" + code + ")");
    }
}
